//
//  YMSafeTest.m
//  niuguwang
//
//  Created by BrightLi on 2021/6/17.
//  Copyright © 2021年 taojinzhe. All rights reserved.
//

#import "YMSafeTest.h"
#import <UIKit/UIKit.h>

@interface Person:NSObject
@end

@implementation Person
@end

@implementation YMSafeTest

+ (void) test
{
//    [self testNSString];
//    [self testNSAttributedString];
//    [self testNSArray];
//    [self testNSMutableArray];
//    [self testNSDictionary];
//    [self testNSMutableDictionary];
//    [self testNSObject];
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
#pragma clang diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic ignored "-Wnonnull"

+ (void) testNSObject
{
    UITableView *anyObject = [UITableView new];
    [anyObject setValue:self forKey:@"AvoidCrash"];
    
    anyObject = [UITableView new];
    [anyObject setValue:self forKeyPath:@"AvoidCrash"];
}

+ (void) testNSString
{
    NSString *str = @"chenfanfang";
    unichar characteristic = [str characterAtIndex:100];
    NSLog(@"--%c--",characteristic);
    
    str = @"chenfanfang";
    NSString *subStr = [str substringFromIndex:100];
    NSLog(@"%@",subStr);
    
    str = @"chenfanfang";
    subStr = [str substringToIndex:100];
    NSLog(@"%@",subStr);
    
    str = @"chenfanfang";
    NSRange range = NSMakeRange(0, 100);
    subStr = [str substringWithRange:range];
    NSLog(@"%@",subStr);
    
    NSString *nilStr = nil;
    str = [str stringByReplacingOccurrencesOfString:nilStr withString:nilStr];
    NSLog(@"%@",str);
    
    str = @"chenfanfang";
    range = NSMakeRange(0, 1000);
    str = [str stringByReplacingOccurrencesOfString:@"chen" withString:@"" options:NSCaseInsensitiveSearch range:range];
    NSLog(@"%@",str);
    
    str = @"chenfanfang";
    range = NSMakeRange(0, 1000);
    str = [str stringByReplacingCharactersInRange:range withString:@"cff"];
    NSLog(@"%@",str);
}

+ (void) testNSAttributedString
{
    NSAttributedString *attr = [[NSAttributedString alloc] initWithString:nil attributes:nil];
    attr = [[NSAttributedString alloc] initWithString:@"hello"];
    NSLog(@"%@", [attr attributedSubstringFromRange:NSMakeRange(1, 10)].string);
    attr = [[NSMutableAttributedString alloc] initWithString:nil attributes:nil];
    attr = [[NSMutableAttributedString alloc] initWithString:@""];
    [attr attributedSubstringFromRange:NSMakeRange(1, 3)];
}
+ (void) testNSDictionary
{
    NSString *nilStr = nil;
    NSDictionary *dict = @{
                           @"name" : @"chenfanfang",
                           @"age" : nilStr
                           };
    NSLog(@"%@",dict);
}

+ (void) testNSMutableDictionary
{
    NSMutableDictionary *dict = @{
                                  @"name" : @"chenfanfang"
                                  
                                  }.mutableCopy;
    NSString *ageKey = nil;
    dict[ageKey] = @(25);
    NSLog(@"%@",dict);
    dict = [NSMutableDictionary dictionary];
    [dict setObject:@(25) forKey:ageKey];
    NSLog(@"%@",dict);
    dict = @{
                                  @"name" : @"chenfanfang",
                                  @"age" : @(25)
                                  }.mutableCopy;
    NSString *key = nil;
    [dict removeObjectForKey:key];
    NSLog(@"%@",dict);
}

+ (void) testNSPlaceholderArray
{
    NSArray *item = nil;
    NSArray *items = @[@"a",@"b", item ,@"c"];
}
+ (void) testNSArray
{
    NSString *nilStr = nil;
    NSArray *array = @[@"chenfanfang", nilStr, @"iOSDeveloper"];
    NSLog(@"%@",array);

    NSArray *arr = @[@"chenfanfang", @"iOS_Dev"];
    NSObject *object = arr[100];
    NSLog(@"object = %@",object);
    
    array = @[@"chenfanfang",@"iOS"];
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:100];
    [array objectsAtIndexes:indexSet];
    
    array = @[@"1", @"2", @"3"];//__NSArrayI
    //NSArray *array = @[@"1"];//__NSSingleObjectArrayI
    //NSArray *array = @[];//NSArray
    NSRange range = NSMakeRange(0, 11);
    __unsafe_unretained id cArray[range.length];
    [array getObjects:cArray range:range];
    
}
// 动态数组测试
+ (void) testNSMutableArray
{
    NSMutableArray *array = @[@"chenfanfang"].mutableCopy;
    NSObject *object = array[2];
    //NSObject *object = [array objectAtIndex:20];
    NSLog(@"object = %@",object);
    array = @[@"chenfanfang"].mutableCopy;
    array[3] = @"iOS";
    array = @[@"chenfanfang", @"iOSDeveloper"].mutableCopy;
    [array removeObjectAtIndex:5];
    array = @[@"chenfanfang"].mutableCopy;
    //test1    beyond bounds
    [array insertObject:@"cool" atIndex:5];
    //test2    insert nil obj
    [array insertObject:nil atIndex:0];
    //test3    insert nil obj
    NSString *nilStr = nil;
    [array addObject:nilStr]; //其本质是调用insertObject:
}

#pragma clang diagnostic pop

@end
